package com.atguigu.juc.t14;

import java.util.Random;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

public class T1 {
    public static void main(String[] args) {
        CountDownLatch countDownLatch = new CountDownLatch(30);
        System.out.println("我是负责锁门的小黄同学");
        for (int i = 1; i < 31; i++) {
            new Thread(()->{
                System.out.println(Thread.currentThread().getName() + "开始上自习");
                try {
                    TimeUnit.SECONDS.sleep(new Random().nextInt(3 + 1));
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                System.out.println(Thread.currentThread().getName() + "下自习，离开教室");
                countDownLatch.countDown();
            }).start();
        }
        try {
            countDownLatch.await();
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        System.out.println("到了关门时间，我要关门了");
    }
}
